#include "tisserialport.h"

#include <QMessageBox>
#include <QDebug>





TisSerialPort::TisSerialPort(QObject *parent) :
    QObject(parent)
{
    timerdly = TIMER_INTERVAL;

    /*
    *BAUD110                110         110
    *BAUD300                300         300
    *BAUD600                600         600
    *BAUD1200              1200        1200
    *BAUD2400              2400        2400
    *BAUD4800              4800        4800
    *BAUD9600              9600        9600
    BAUD14400            14400        9600
    *BAUD19200            19200       19200
    *BAUD38400            38400       38400
    BAUD56000            56000       38400
    *BAUD57600            57600       57600
    *BAUD115200          115200      115200
    BAUD128000          128000      115200
    */
    m_baudRate.insert(110, BAUD110);
    m_baudRate.insert(300, BAUD300);
    m_baudRate.insert(600, BAUD600);
    m_baudRate.insert(1200, BAUD1200);
    m_baudRate.insert(2400, BAUD2400);
    m_baudRate.insert(4800, BAUD4800);
    m_baudRate.insert(9600, BAUD9600);
    m_baudRate.insert(14400, BAUD14400);
    m_baudRate.insert(19200, BAUD19200);
    m_baudRate.insert(38400, BAUD38400);
    m_baudRate.insert(56000, BAUD56000);
    m_baudRate.insert(57600, BAUD57600);
    m_baudRate.insert(115200, BAUD115200);
    m_baudRate.insert(128000, BAUD128000);

  //  BaudRateType v = m_baudRate[300];
    //设置读取计时器
    timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT(readMyCom()));
}


bool TisSerialPort::OpenPort(int nPort, ulong dwBaudRate, QueryMode mode)
{
    QString portName;

#ifdef _TTY_POSIX_
    portName.sprintf("/dev/ttyS%d", nPort-1);   //获取串口名
#else
    portName.sprintf("%d", nPort);      //获取串口名
#endif

//    qDebug() << "portName============>" << portName << nPort;
    myCom = new QextSerialPort(portName, QextSerialBase::Polling);
    //这里QextSerialBase::QueryMode应该使用QextSerialBase::Polling

    if(myCom->open(QIODevice::ReadWrite))
    {
        logger()->info("Success to open TIS serialport %1\n", portName);

//       qDebug("open success");
        //QMessageBox::information(this, tr("打开成功"), tr("已成功打开串口 ") + portName, QMessageBox::Ok);
    }
    else
    {
        logger()->error("Fail to open TIS serialport %1\n", portName);

//        qDebug("open fail");
      // QMessageBox::critical(this, tr("打开失败"), tr("未能打开串口 ") + portName + tr("\n该串口设备不存在或已被占用"), QMessageBox::Ok);
        return false;
    }

    //波特率转码


    //设置波特率
    myCom->setBaudRate(m_baudRate[dwBaudRate]);

    //设置数据位
//    myCom->setDataBits((DataBitsType)ui->dataBitsComboBox->currentIndex());

//    //设置校验
//    myCom->setParity((ParityType)ui->parityComboBox->currentIndex());

//    //设置停止位
//    myCom->setStopBits((StopBitsType)ui->stopBitsComboBox->currentIndex());

    //开启读取定时器
    timer->start(timerdly);
    //设置数据流控制
    myCom->setFlowControl(FLOW_OFF);
    //设置延时
    myCom->setTimeout(TIME_OUT);

    //ui->statusBar->showMessage(tr("打开串口成功"));

    return true;
}

bool TisSerialPort::OpenPort(/*const QString & name*/QString name, ulong dwBaudRate, QueryMode mode)
{
    QString portName;

#ifdef _TTY_POSIX_
    portName.append("/dev/");   //获取串口名
    portName.append(name);
#else
    portName = name;      //获取串口名
#endif

    myCom = new QextSerialPort(portName, QextSerialBase::Polling);
    //这里QextSerialBase::QueryMode应该使用QextSerialBase::Polling
//    qDebug() << "portName============>" << portName << "dwBaudRate:" << dwBaudRate;

    if(myCom->open(QIODevice::ReadWrite))
    {
        logger()->info("Success to open TIS serialport %1 \n", portName);

//       qDebug("open success");
        //QMessageBox::information(this, tr("打开成功"), tr("已成功打开串口 ") + portName, QMessageBox::Ok);
    }
    else
    {
        logger()->error("Fail to open TIS serialport %1\n", portName);

//        qDebug("open fail");
      // QMessageBox::critical(this, tr("打开失败"), tr("未能打开串口 ") + portName + tr("\n该串口设备不存在或已被占用"), QMessageBox::Ok);
        return false;
    }

    //波特率转码


    //设置波特率
    myCom->setBaudRate(m_baudRate[dwBaudRate]);

    //设置数据位
//    myCom->setDataBits((DataBitsType)ui->dataBitsComboBox->currentIndex());

//    //设置校验
//    myCom->setParity((ParityType)ui->parityComboBox->currentIndex());

//    //设置停止位
//    myCom->setStopBits((StopBitsType)ui->stopBitsComboBox->currentIndex());

    //开启读取定时器
    timer->start(timerdly);
    //设置数据流控制
    myCom->setFlowControl(FLOW_OFF);
    //设置延时
    myCom->setTimeout(TIME_OUT);
    //ui->statusBar->showMessage(tr("打开串口成功"));

    return true;
}

//关闭串口
void TisSerialPort::ClosePort()
{

    myCom->close();
    delete myCom;
    timer->stop();
}

//读取数据
void TisSerialPort::readMyCom()
{
    QByteArray data = myCom->readAll();

    if(!data.isEmpty())
    {
         // Send(data);
        emit dataArrive(data.data(), data.size());
    }
}

qint64  TisSerialPort::Send(const char *data, qint64 len)
{
    return myCom->write(data, len);
}

qint64  TisSerialPort::Send(const char *data)
{
    return myCom->write(data);
}

qint64  TisSerialPort::Send(const QByteArray &data)
{
    return myCom->write(data);
}

